<?php
session_start();
//包含头
include '../PHP/head.php';
?>
<?php
//包含bootstrap的顶端按钮
include '../PHP/menu.php';
?>
<br />
<br />
<p class="text-center lead">新增模块报价编号</p>
<br />
<form class="form-inline" action = "../PHP/add_project_code.php" method = "post">
<div class="form-group">
    <label for="project_code">模块报价编号：</label>
    <input type = "text"  class="form-control" placeholder="MP15-XXX" name = "project_code" />
 </div>
<div class="form-group">
    <label for="project_name">项目名称：</label>
    <input type = "text"  class="form-control" placeholder="项目名称" name = "project_name" />
 </div>
 
 <div class='form-group'>
	<label for='sale'>销售员：</label>
	<select class='form-control' name='sale'>
 <?php
 //搜索销售员，position like "%SA%"
 //引用必要的函数
require_once ('../FNS/db_fns.php');


//连接数据库并搜索数据

$db= db_connect();

$query="select * from users where position like '%SA%'";
$result=$db->query ($query);

//获取记录数量
$num_results=$result->num_rows;

//循环输出选项
for ($i=0; $i < $num_results; $i++) {
 
	$row=$result->fetch_assoc ();
	
	$user_id = stripslashes ($row['id']);
	$user_name = stripslashes ($row['name']);
	
	echo "<option value='".$user_id."'>".$user_name."</option>";
}

$result->free();
$db->close();

?>
	</select>
</div>
 <button type="submit" class="btn btn-default">新增</button>
</form>


<?php
//引用必要的函数
require_once ('../FNS/db_fns.php');

if(isset($_POST['project_code'])) {
    
//创建变量并清除前后空格
$project_code = trim ($_POST['project_code']);
$project_name = trim ($_POST['project_name']);
$sale = trim ($_POST['sale']);

//判断是否为空
if (!$project_code || !$project_name || !$sale){
	echo "<br />";
	echo "<p>所有输入不能为空，请重新输入！</P>";
	echo "</div>";
	echo "</body>";
	exit;
}



//数据过滤和处理
//加引号避免输入数据库出错

if (! get_magic_quotes_gpc()) {
$project_code=addslashes ($project_code);
$project_name=addslashes ($project_name);
$sale=addslashes ($sale);
}

//连接数据库并插入数据

$db= db_connect();

$query="insert into projects (`code`, `name`, `flag_del`, `sale`) values
  ('".$project_code."', '".$project_name."', '1', '".$sale."')";
$result=$db->query ($query);
if ($result){
	echo "<br />";
	echo "添加成功！";
}else{
	echo "<br />";
	echo"出错，没有添加！";
}


$db->close();
}

?>


</div>
</body>
</html>
